#!/usr/bin/env python

import sys
import struct

def hex_change(n, little=True, byte=1, size=4):
	s = "%%0%dX"%(size*2)
	s = s%(n)
	step = byte*2
	if len(s) % (size*2) != 0:
		raise Exception("error parse, overflow 0x%08X"%(n))
	d = []
	if little:
		for i in xrange(len(s), 0, -step):
			d.append(s[i-step:i])
	else:
		for i in xrange(0, len(s), step):
			d.append(s[i:i+step])
	return tuple(d)

def uint_2_hex_8_big(n):
	return hex_change(n, little=False, byte=1, size=4)

def uint_2_hex_8_little(n):
	return hex_change(n, little=True, byte=1, size=4)

def parse(bin_file, hex_file):
	ofs = open(hex_file, "w")
	with open(bin_file, "rb") as f:
		while True:
			r = f.read(4)
			if r != "":
				n = struct.unpack("I", r)
				k = uint_2_hex_8_little(n)
				ofs.write("\n".join(k)+"\n")
			else:
				break
	ofs.close()

def main():
	if len(sys.argv) != 3:
		print "usage: %s input_bin_file_path out_put_hex_file_path"%(sys.argv[0])
		sys.exit(1)
	bin_file=sys.argv[1]
	hex_file=sys.argv[2]
	# bin_file="avk_boot.bin"
	# hex_file="avk_boot.hex"
	parse(bin_file, hex_file)

if __name__ == '__main__':
	main()
